#!/usr/bin/python

#Created by turk 2016-02-14
#Test DELAY [PYTHON]
#Version 1.0 

import os
import commands
import sys
import logging
import time

WORKPATH='/home/bsmp/work'
SPARKLIB=WORKPATH + '/lib/spark'
LOGPATH=WORKPATH + '/log'
LOGFILE=LOGPATH + '/delaytest.log'

#HDFS
SDSDBPATH="/user/hive/warehouse/sds.db"

#LOCAL
DATAPATH="/home/bsmp/work/data/delay"

#Logger
logging.basicConfig(level=logging.DEBUG,  
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',  
                    datefmt='%Y-%m-%d %H:%M:%S',  
                    filename=LOGFILE,  
                    filemode='w')  
console = logging.StreamHandler()  
console.setLevel(logging.INFO)  
# set print format
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')  
console.setFormatter(formatter)  
#
logging.getLogger('').addHandler(console)  
logger1 = logging.getLogger('test.delay')                     
                    


def ExportData(DESTIP,SOURCEIP,DATE,STARTHOUR,ENDHOUR):
    ISOTIMEFORMAT='%Y%m%d%X'
    STARTTIME = time.strftime(ISOTIMEFORMAT,time.localtime())
    
    SQL="""hive -e \"insert overwrite local directory '""" + DATAPATH  + """/""" + STARTTIME + """'
        row format delimited
        fields terminated by ','
        select from_unixtime(accesstime,'yyyy-MM-dd HH:mm') t,sourceip,count(*) pv from idc_access_log 
        where destip = '"""+ DESTIP +"""' and dt = """+DATE+""" and sourceip = '"""+SOURCEIP+"""' and hour between """+STARTHOUR+""" and """+ENDHOUR+"""
        group by from_unixtime(accesstime,'yyyy-MM-dd HH:mm'),sourceip
        order by t desc \""""

    logger1.info(SQL)
    RET=commands.getstatusoutput(SQL)
    logger1.info(RET)
    

if __name__ == '__main__':
    print sys.argv  
    if len(sys.argv) <5:
        print "Usage:<DESTIP> <SOURCEIP> <DATE> <STARTHOUR> <ENDHOUR>"
        sys.exit()
    
    DESTIP=sys.argv[1]
    SOURCEIP=sys.argv[2]
    DATE=sys.argv[3]
    STARTHOUR=sys.argv[4]
    ENDHOUR=sys.argv[5]
        
    ExportData(DESTIP,SOURCEIP,DATE,STARTHOUR,ENDHOUR)
     
    print 'COMPLATE'
